package com.yuji.aacar.base;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;

import com.yuji.aacar.bean.response.VersionResponse;
import com.yuji.aacar.controller.splash.SplashActivity;
import com.yuji.aacar.core.cores.event.CoreEventBusMessage;
import com.yuji.aacar.core.cores.services.CoreServiceResponse;
import com.yuji.aacar.core.mvvm.appbase.AppRoute;
import com.yuji.aacar.core.mvvm.appbase.CoreBaseFragment;
import com.yuji.aacar.service.BaseServiceMediator;
import com.yuji.aacar.widgets.dialogue.MyAlertDialogue;

/**
 * <li>Package:com.yuji.aacar.base</li>
 * <li>Author: lining </li>
 * <li>Date: 2018/2/28</li>
 * <li>Description:   </li>
 */

public class BaseFragment extends CoreBaseFragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void alreadyBindBaseViewModel() {
        super.alreadyBindBaseViewModel();
//        requestVersionCheck();
    }


    @Override
    public void handleMessage(CoreEventBusMessage message) {
        super.handleMessage(message);
        if (message.getMessageCode().contentEquals(String.valueOf(EventConst.RELOGIN))) {
            presentControllerFromFragment(SplashActivity.class);
            getActivity().finish();
        }
    }


    @Override
    public void requestSuccess(CoreServiceResponse response) {
        super.requestSuccess(response);
        if (response.getRequestCode() == BaseServiceMediator.COMPARE_APP_VERSION_CODE){
            VersionResponse versionResponse = (VersionResponse) response.getResponse();

        }
    }

    @Override
    public void requestFailed(CoreServiceResponse response) {
        super.requestFailed(response);
//        if (response.getResultCode() == Code.LOGIN_FAILED) {
//            CoreToast.showToast(getActivity(), response.getMessage(), Toast.LENGTH_SHORT);
//            AutoConfig.logout();
//        } else {
//            if (!TextUtils.isEmpty(response.getMessage())) {
//                ToastUtil.showToast(response.getMessage());
//            }
//        }
    }

    /**
     * Activity跳转
     *
     * @param activity
     */
    public void presentControllerFromFragment(Class activity) {
        AppRoute.getInstance().nextControllerForFragment(this, activity, AppRoute.WITHOUT_RESULTCODE);
    }

    /**
     * Activity跳转
     *
     * @param activity
     * @param intent
     */
    public void presentControllerFromFragment(Class  activity, Intent intent) {
        AppRoute.getInstance().nextControllerForFragmentWithIntent(this, activity, AppRoute.WITHOUT_RESULTCODE, intent);
    }

    /**
     * Activity跳转
     *
     * @param activity
     * @param intent
     * @param requestCode
     */
    public void presentControllerFromFragment(Class<? extends BaseFragmentActivity> activity, Intent intent, int requestCode) {
        AppRoute.getInstance().nextControllerForFragmentWithIntent(this, activity, requestCode, intent);
    }

    /***
     * 显示Loading框
     ***/
    public void showProgress() {
        if (getActivity() instanceof BaseFragmentActivity) {
//            ((BaseFragmentActivity) getActivity()).showProgress();
        }
    }

    /***
     * 关闭Loading框
     ***/
    public void dismissProgress() {
        if (getActivity() instanceof BaseFragmentActivity) {
//            ((BaseFragmentActivity) getActivity()).dismissProgress();
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }


    public void showAlertDialogue (Context context, String title, String singleMessage, String confirmMessage) {
        MyAlertDialogue.Builder ibuilder = new MyAlertDialogue.Builder(context);
        ibuilder.setTitle(title);
        ibuilder.setMessage(singleMessage);
        ibuilder.setPositiveButton(confirmMessage, null);
        ibuilder.create().show();

    }

    public void showAlertSingleButtonDialogue (Context context, String title, String singleMessage, String confirmMessage, DialogInterface.OnClickListener confirmListener) {
        MyAlertDialogue.Builder ibuilder = new MyAlertDialogue.Builder(context);
        ibuilder.setTitle(title);
        ibuilder.setMessage(singleMessage);
        ibuilder.setPositiveButton(confirmMessage, confirmListener);
        ibuilder.create().show();

    }

    public void showAlertDialogue (Context context,String cancelMessage, String singleMessage, String confirmMessage, DialogInterface.OnClickListener confirmListener) {
        MyAlertDialogue.Builder ibuilder = new MyAlertDialogue.Builder(context);
        ibuilder.setMessage(singleMessage);
        ibuilder.setPositiveButton(confirmMessage, confirmListener);
        ibuilder.setNegativeButton(cancelMessage, null);
        ibuilder.create().show();
    }

    public void showAlertDialogue (Context context,String cancelMessage, String titleMessage, String singleMessage, String confirmMessage,DialogInterface.OnClickListener confirmListener) {
        MyAlertDialogue.Builder ibuilder = new MyAlertDialogue.Builder(context);
        ibuilder.setTitle(titleMessage);
        ibuilder.setMessage(singleMessage);
        ibuilder.setPositiveButton(confirmMessage, null);
        ibuilder.setNegativeButton(cancelMessage, null);
        ibuilder.create().show();
    }

    public void showFragemt () {
    }

}
